﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TWZAV12
    {
        #region ==RPAUF===========
        public static readonly DependencyProperty RPAUFProperty = DependencyProperty.Register("RPAUF",
            typeof(string), typeof(TWZAV12),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRPAUFChanged));
        public string RPAUF
        {
            get
            {
                return (string)GetValue(RPAUFProperty);
            }
            set
            {
                SetValue(RPAUFProperty, value);
            }
        }
        private static void OnRPAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWZAV12;
            if (src != null)
            {
                src.RPAUF = (string)e.NewValue;
            }
        }
        #endregion
        public TWZAV12()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        } 
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
            if (!IsRPMode)
                return;
            if (dwMaster == null)
                return;
            RPMode();
        }
        bool IsRPMode = false;
        public void RPMode()
        {           
            dwMaster["WBBAUFTYP"] = RPAUF.Substring(0, 1);
            dwMaster["WBBAUFKRS"] = RPAUF.Substring(1,2);
            dwMaster["WBBAUFLFD"] = RPAUF.Substring(3, 6);
            dwMaster["WBBAUFPOS"] = RPAUF.Substring(9, 3);
            dwMaster["WBBTP"] = "0001";
            dwMaster["WBBFOLGE"] = Helpers.Helper.Right("00" + FOL.ToString(), 2);
            dwMaster["WBBAFOLGEVON"] = "000";
            dwMaster["WBBRAFOLGE"] = "000";
            dwMaster["WBBMAFOLGE"] = "000";
            FOL++;
            TMV.UWB19(dwMaster);
            IsWZChg = false;
            IsWZChg = true;
        }
       
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            base.NewCommand(sender, e);
            if (RPAUF.Trim().Length == 0)
                return;
            if (!IsRPMode)
            {
                IsRPMode = true;
                vwMaster.View.MoveCurrentTo(-1); 
            } 
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            IsRPMode = false;
            FOL =1;
            base.SaveCommand(sender, e);
        }

        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear(); 
            if (EXAUF.Length < 14)
                return;
            string wdsa = EXAUF.Substring(0, 1);
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;             
            dwData["WBBAUFTYP"] = "V";
            dwData["WBBAUFKRS"] = EXAUF.Substring(0, 2);
            dwData["WBBAUFLFD"] = EXAUF.Substring(2, 6);
            dwData["WBBAUFPOS"] = EXAUF.Substring(8, 3);
            dwData["WBBFILLER"] = " AND WBBMAFOLGE>='" + EXAUF.Substring(11, 3)+"'";
            SResult = TMV.QWB29(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
        } 
    }
}
